import { pair } from "../../config/config";
import bus from "../../eventBus/eventBus";
import tableColumn from "../tableColumn.vue";
export const tableMixins = {
  model: {
    prop: "tableData",
    event: "change",
  },
  props: {
    tableData: Array,
    align: {
      type: String,
      default: "center",
    },
    width: {
      type: String,
      default: "150",
    },
    size: {
      type: String,
      default: "mini",
    },
    headList: {
      type: Array,
      default: () => [],
    },
  },
  components: { tableColumn },
  data() {
    return {
      height: 0,
      selectedList: [], // 已选择人员
      // modifiableAttribute: [], // 可修改属性
    };
  },
  computed: {

    /** 动态表头 */
    tableTh() {
      const newArr = [];
      this.headList.forEach((item) => {
        let [key, type] = pair.getNameMatch(item.name);

        const formula = [2, 6].includes(item.columnRule) ? this.t_getCellFormula(item.columnId) : "";
        if (item.columnRule == 4) {
          // 手动录入
          type = item.itemNamePy == 'gerenxinxi' ? "input" : "number";
          
        }
        if (item.rowCode == 2) {
          const row = newArr.find((o) => o.itemId == item.itemId);
          if (["养老", "失业", "医疗", "工伤", "生育"].includes(item.name)) {
            [key, type] = pair.getNameMatch(row.name.substr(0, 2) + item.name);
          }
          if (row) {
            if (!row?.children?.length) row.children = [];
            row.children.push({ ...item, key: key || item.namePy, type, formula });
          }
        } else newArr.push({ ...item, key: key || item.namePy, type, children: [], formula });
      });
      // console.table(JSON.parse(JSON.stringify(newArr)));
      return newArr;
    },

    /** 是否有二级表头 */
    isTowRow() {
      return this.tableTh.some((item) => item?.children?.length);
    },

    /** 可修改的属性 */
    modifiableAttribute() {
      let newArr = [];
      this.tableTh.forEach((item) => {
        if (item.type) newArr.push(item);
        if (item?.children?.length)
          item.children.forEach((o) => {
            if (o.type) newArr.push(o);
          });
      });
      // const newArr = this.tableTh.filter((item) => item.type == "number");
      console.log(newArr);
      return newArr || [];
    },
  },
  methods: {

    /** 获取公式 */
    t_getCellFormula: (id) => {
      return bus.formulaArr?.find((item) => item.id == id)?.formulaDetail;
    },
    t_setHeight() {
      this.$nextTick(() => {
        this.height = this.$refs.wrapper.offsetHeight - 1;
      });
    },
    t_init() {
      this.t_reloadData();
      this.t_setHeight();
    },
    t_handleSelectionChange(e) {
      this.selectedList = e;
      console.log(e);
    },
    t_filterChange(e) {
      console.log(e);
    },
    t_reloadData() {
      this.$refs.table.reloadData(this.tableData);
    },
    t_doLayout() {
      this.$refs?.table?.doLayout();
    },
  },
  mounted() {
    this.t_init();
  },
};
